package com.lx.web.servlet;

import com.lx.bean.Content;
import com.lx.bean.ResultInfo;
import com.lx.service.ContentService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/content")
public class ContentController {
    @Autowired
    ContentService service;

    @RequestMapping("/findbykey/{id}")
    public Content findbyKeyContent(@PathVariable Integer id) {
        return service.findall(id);
    }

    @RequestMapping("/publish/{id}")
    public void publish(@PathVariable Integer id, HttpServletResponse response) throws IOException {
        Cookie cookie = new Cookie("contentBodyId",id+"");
        response.addCookie(cookie);
        response.sendRedirect("/LxCommunity/html/Publish.html");
    }

    @RequestMapping("/instercontent")
    @ResponseBody
    public ResultInfo insterContent(@RequestBody Content content) {
        ResultInfo info = new ResultInfo();
        System.out.println("content = " + content.getContent());
        if (service.instercontent(content) > 0) {
            info.setFlag(true);
        } else {
            info.setFlag(false);

        }

        return info;
    }

    @RequestMapping("/hot")
    public List<Content> findbyhot() {

        return service.findhot();
    }


    @ResponseBody
    @RequestMapping("/selAll")
    public List<Content> selAll() {
        return service.selAll();
    }


    @ResponseBody
    @RequestMapping("/selByFbid/{id}")
    public List<Content> selByFbid(@PathVariable Integer id) {
        return service.selByFbid(id);
    }
}
